package com.idrive.database.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.idrive.database.DatabaseHelper;
import com.idrive.database.DatabaseUtility;
import com.idrive.database.dto.CommonData;

public class CommonDataDao {

	// Database fields
	private SQLiteDatabase database;

	private String[] allColumns = { DatabaseUtility.COLUMN_COMMON_DATA_OPTION, DatabaseUtility.COLUMN_COMMON_DATA_SMS,
	        DatabaseUtility.COLUMN_COMMON_DATA_READ_SMS, DatabaseUtility.COLUMN_COMMON_DATA_TRACK_PATH };

	public CommonDataDao(Context context) {
		DatabaseHelper.init(context);
		database = DatabaseHelper.getInstance().getWritableDatabase();
	}

	public void updateCommonData(CommonData commonData) {
		ContentValues values = new ContentValues();
		values.put(DatabaseUtility.COLUMN_COMMON_DATA_OPTION, commonData.getOption());
		values.put(DatabaseUtility.COLUMN_COMMON_DATA_SMS, commonData.getSms());
		values.put(DatabaseUtility.COLUMN_COMMON_DATA_READ_SMS, commonData.getReadSms());
		values.put(DatabaseUtility.COLUMN_COMMON_DATA_TRACK_PATH, commonData.getTrackPath());

		Cursor cursor = database.query(DatabaseUtility.TABLE_COMMON_DATA, allColumns, null, null, null, null, null);

		if (cursor == null) {
			/*
			 * database.execSQL("INSERT INTO " + DatabaseUtility.TABLE_COMMON_DATA + " (" +
			 * DatabaseUtility.COLUMN_COMMON_DATA_OPTION + "," + DatabaseUtility.COLUMN_COMMON_DATA_SMS + "," +
			 * DatabaseUtility.COLUMN_COMMON_DATA_READ_SMS + "," + DatabaseUtility.COLUMN_COMMON_DATA_TRACK_PATH +
			 * ") VALUES('" + commonData.getOption() + "'," + commonData.getSms() + "," + commonData.getReadSms() + ","
			 * + commonData.getTrackPath() + ");");
			 */
			database.insert(DatabaseUtility.TABLE_COMMON_DATA, null, values);
		} else {
			database.update(DatabaseUtility.TABLE_COMMON_DATA, values, null, null);
			/*
			 * database.execSQL("UPDATE " + DatabaseUtility.TABLE_COMMON_DATA + " SET " +
			 * DatabaseUtility.COLUMN_COMMON_DATA_OPTION + "=" + commonData.getOption() + ", " +
			 * DatabaseUtility.COLUMN_COMMON_DATA_SMS + "=" + commonData.getSms() + ";");
			 */
		}

		database.close();
	}

	public CommonData getCommonData() {

		CommonData data = new CommonData();
		Cursor cursor = database.query(DatabaseUtility.TABLE_COMMON_DATA, allColumns, null, null, null, null, null);

		cursor.moveToFirst();
		// //while (!cursor.) {

		data.setOption(cursor.getString(cursor.getColumnIndex(DatabaseUtility.COLUMN_COMMON_DATA_OPTION)));
		data.setSms(cursor.getString(cursor.getColumnIndex(DatabaseUtility.COLUMN_COMMON_DATA_SMS)));
		data.setReadSms(cursor.getString(cursor.getColumnIndex(DatabaseUtility.COLUMN_COMMON_DATA_READ_SMS)));
		data.setTrackPath(cursor.getString(cursor.getColumnIndex(DatabaseUtility.COLUMN_COMMON_DATA_TRACK_PATH)));
		// }
		// Make sure to close the cursor
		cursor.close();
		return data;
	}

}
